var blocks= new Array();
var teamNum:int;
var height:int;
var numOfBlocks:int;
var attkCounter:int=0;
var numOfLogic:int=0;
var numOfDeception:int=0;
var numOfPassion:int=0;
var numOfEthics:int=0;


//var guiLayer:GUILayer;
var trap:int;

function Update () {
	numOfBlocks=blocks.length;
	var curHeight=0;
	for(var curBlock in blocks)
	{
		curHeight+=curBlock.blockSize;
		
	}
	this.height=curHeight;
	//guiLayer.Tower1Height.GUIText.Text="Current Height:" + height;

}

function setTrap(type:int){
	trap=type;
}

function getTrap(){
	return trap;
}

function setAttackCounter(type:int)
{
	attkPower=type;
}


function DamageTopBlock(inDmg)
{	var check:boolean=false;
	if (attkCounter==50)
	{	
		inDmg=inDmg/2;
		attkCounter=0;
	}
	else if (attkCounter==100)
	{
		inDmg=0;
		attkCounter=0;
	}
	if(blocks.length==0)
	{
		print("No more blocks in stack to hit!");
	}
	else
	{
		while(check==false)
		{
			//get stats of block on top of stack
			curTopBlockSize=blocks[(blocks.length-1)].GetBlockSize();
			curTopBlockArmor=blocks[(blocks.length-1)].GetBlockArmor();
			//calculate damage needed to destroy block
			curTopBlockHealth=curTopBlockSize*curTopBlockArmor;
			if (curTopBlockHealth<=inDmg)
			{
				inDmg=inDmg-curTopBlockHealth;
				if (blocks[(blocks.length-1)].blockType==1) numOfLogic--;
				if (blocks[(blocks.length-1)].blockType==2) numOfPassion--;
				if (blocks[(blocks.length-1)].blockType==3) numOfEthics--;
				if (blocks[(blocks.length-1)].blockType==4) numOfDeception--;
				DestroyImmediate(blocks[(blocks.length-1)].gameObject,true);
				blocks.RemoveAt(blocks.length-1);
				if(blocks.length==0) 
				{
					check=true;
					print("Leftover dmg, but no more blocks!");
				}
				
			}
			else
			{
				
				
					//Debug.Log("Block Health Before" + blocks[(blocks.length-1)].blockSize);
					blocks[(blocks.length-1)].SetBlockSize(curTopBlockSize-(inDmg/curTopBlockArmor));
					//Debug.Log("Block Health After" + blocks[(blocks.length-1)].blockSize);
					check=true;
				
			}
		}
	}
		
}

function DamageBottomBlock(inDmg)
{	var check:boolean=false;
	if (attkCounter==50)
	{	
		inDmg=inDmg/2;
		attkCounter=0;
	}
	else if (attkCounter==100)
	{
		inDmg=0;
		attkCounter=0;
	}
	if(blocks.length==0)
	{
		print("No more blocks in stack to hit!");
	}
	else
	{
		while(check==false)
		{
			//get stats of block on top of stack
			curTopBlockSize=blocks[0].GetBlockSize();
			curTopBlockArmor=blocks[0].GetBlockArmor();
			//calculate damage needed to destroy block
			curTopBlockHealth=curTopBlockSize*curTopBlockArmor;
			if (curTopBlockHealth<=inDmg)
			{
				inDmg=inDmg-curTopBlockHealth;
				if (blocks[0].blockType==1) numOfLogic--;
				if (blocks[0].blockType==2) numOfPassion--;
				if (blocks[0].blockType==3) numOfEthics--;
				if (blocks[0].blockType==4) numOfDeception--;
				DestroyImmediate(blocks[0].gameObject,true);
				blocks.RemoveAt(0);
				if(blocks.length==0) 
				{
					check=true;
					print("Leftover dmg, but no more blocks!");
				}
				
			}
			else
			{
				
				
					//Debug.Log("Block Health Before" + blocks[0].blockSize);
					blocks[0].SetBlockSize(curTopBlockSize-(inDmg/curTopBlockArmor));
					//Debug.Log("Block Health After" + blocks[0].blockSize);
					check=true;
				
			}
		}
	}
		
}


function AddBlock(inBlock)
{
	this.blocks.Add(inBlock);
	if (blocks[0].blockType==1) numOfLogic++;
	if (blocks[0].blockType==2) numOfPassion++;
	if (blocks[0].blockType==3) numOfEthics++;
	if (blocks[0].blockType==4) numOfDeception++;
	//Debug.Log("Team "+ teamNum + "'s Tower is now at height: " + blocks.length);
}

function DecayDeceptionBlocks()
{
	numOfBlocks=blocks.length;
	var i;
	for(i=0;i<numOfBlocks;i++){
		curBlock=blocks[i];
		if(curBlock.blockType==4){ //Deception type
			curBlock.blockSize=curBlock.blockSize-1;
		}
			

	}
}